package com.xxl.job.admin.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xxl.job.admin.controller.annotation.PermessionLimit;
import com.xxl.job.admin.core.model.XinApi;
import com.xxl.job.admin.service.XinApiService;
import com.xxl.job.core.biz.model.ReturnT;

/**
 * 外调api 管理模块
 * 
 * @author zhouzhe
 *
 */

@Controller
@RequestMapping("/url")
public class UrlManageController {
 
	@Resource
	public XinApiService xinApiService;

	@RequestMapping("/")
	public String index(Model model)
	{
		return "url/list";
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object>list(HttpServletRequest request)
	{
		String sta = request.getParameter("start");
		int start = sta==null ? 0 : Integer.parseInt(sta);
		return xinApiService.getApiList(start);
	} 
	
	@RequestMapping("/add")
	@ResponseBody
	public ReturnT<Map<String, Object>> addUrl(XinApi xinApi)
	{
		if(xinApiService.addApi(xinApi))
		{
			return new ReturnT<Map<String, Object>>(ReturnT.SUCCESS_CODE, null);
		} else {
			return new ReturnT<Map<String, Object>>(ReturnT.FAIL_CODE, "添加失败");
		}
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public ReturnT<Map<String, Object>> deleteUrl(int id)
	{
		if(xinApiService.deleteApi(id)){
			return new ReturnT<Map<String, Object>>(ReturnT.SUCCESS_CODE, "删除api成功");
		} else {
			return new ReturnT<Map<String, Object>>(ReturnT.FAIL_CODE, "删除api失败");
		}
	}
	
}
